package bb.lanxing.calc.calculator;

import android.util.Log;

import com.github.mikephil.charting.utils.Utils;

import bb.lanxing.calc.data.CadencePoint;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.util.CommonUtil;

public class CadenceCalc {
    final static private String TAG = "CadenceCalc";
    private static final int VALID_INTERVAL = 3000;
    private CadencePoint lastCadencePoint;
    private long lastValidCadenceTime;
    private long lastValidWheelTime;
    private PTAltitudeCalc ptAltitudeCalc = new PTAltitudeCalc();

    public boolean calc(CadencePoint cadencePoint, boolean isCad01) {
        int cadence;
        double d;
        double d2;
        int i;
        double speed;
        int pressure;
        CadencePoint cadencePoint2 = this.lastCadencePoint;
        if (cadencePoint2 == null) {
            this.lastCadencePoint = cadencePoint;
            return false;
        }
        int sumCadence = cadencePoint2.getSumCadence();
        int sumCadence2 = cadencePoint.getSumCadence();
        int cadenceTime = this.lastCadencePoint.getCadenceTime();
        int cadenceTime2 = cadencePoint.getCadenceTime();
        int do16BitDiff = CommonUtil.do16BitDiff(sumCadence2, sumCadence);
        double do16BitDiff2 = CommonUtil.do16BitDiff(cadenceTime2, cadenceTime) / 1024.0f;
        if (cadenceTime > 0 && cadenceTime2 > 0 && do16BitDiff > 0 && do16BitDiff2 > Utils.DOUBLE_EPSILON) {
            this.lastValidCadenceTime = System.currentTimeMillis();
            int ceil = (int) Math.ceil((do16BitDiff / do16BitDiff2) * 60.0d);
            int cadence2 = this.lastCadencePoint.getCadence();
            if (!isCad01 && cadence2 <= 256 && cadence2 >= 0) {
                int i2 = ceil - cadence2;
                if (i2 > 50) {
                    ceil = cadence2 + 50;
                }
                if (i2 < -50) {
                    ceil = cadence2 - 50;
                }
            }
            cadence = Math.min(ceil, 256);
        } else {
            if (System.currentTimeMillis() - this.lastValidCadenceTime <= 3000) {
                cadence = this.lastCadencePoint.getCadence();
            }
            cadence = 0;
        }
        Log.v(TAG, "CadenceCalc, cadence = " + cadence);
        cadencePoint.setCadence(cadence);
        double sumWheel = (double) (((float) (cadencePoint.getSumWheel() - this.lastCadencePoint.getSumWheel()))
                * RemoteSharedPreference.getInstance().getFloat(SPConstant.KEY_WHEEL_LENGTH, 2.171f));
        int wheelTime = this.lastCadencePoint.getWheelTime();
        int wheelTime2 = cadencePoint.getWheelTime();
        double do16BitDiff3 = CommonUtil.do16BitDiff(wheelTime2, wheelTime) / 1024.0f;
        if (wheelTime > 0 && wheelTime2 > 0 && sumWheel > Utils.DOUBLE_EPSILON && do16BitDiff3 > Utils.DOUBLE_EPSILON) {
            this.lastValidWheelTime = System.currentTimeMillis();
            speed = sumWheel / do16BitDiff3;
            d2 = sumWheel;
            i = (int) Math.ceil(60.0d * speed);
        } else if (System.currentTimeMillis() - this.lastValidWheelTime <= 3000) {
            i = this.lastCadencePoint.getWheelRpm();
            speed = this.lastCadencePoint.getSpeed();
            d2 = this.lastCadencePoint.getDistance();
        } else {
            d = 0.0d;
            d2 = 0.0d;
            i = 0;
            Log.d(TAG, "CadenceCalc, wheelRpm = " + i + ", speed = " + d + ", distance = " + d2);
            cadencePoint.setWheelRpm(i);
            cadencePoint.setSpeed(d);
            cadencePoint.setDistance(d2);
            pressure = cadencePoint.getPressure();
            int temperature = cadencePoint.getTemperature();
            if (pressure > 0) {
                float f = pressure / 4096.0f;
                float f2 = temperature / 100.0f;
                float altitude = this.ptAltitudeCalc.getAltitude(1013.25f, f, f2);
                boolean isCalibrated = this.ptAltitudeCalc.isCalibrated();
                cadencePoint.setRealPressure(f);
                cadencePoint.setRealTemperature(f2);
                cadencePoint.setAltitude(altitude);
                cadencePoint.setCalibrated(isCalibrated);
            }
            if (cadencePoint.getSumCadence() == -1) {
                cadencePoint.setSumCadence(sumCadence);
            }
            if (cadencePoint.getCadenceTime() == -1) {
                cadencePoint.setCadenceTime(cadenceTime);
            }
            if (cadencePoint.getSumWheel() == -1) {
                cadencePoint.setSumWheel(this.lastCadencePoint.getSumWheel());
            }
            if (cadencePoint.getWheelTime() == -1) {
                cadencePoint.setWheelTime(wheelTime);
            }
            this.lastCadencePoint = cadencePoint;
            return true;
        }
        d = speed;
        Log.v(TAG, "CadenceCalc, wheelRpm = " + i + ", speed = " + d + ", distance = " + d2);
        cadencePoint.setWheelRpm(i);
        cadencePoint.setSpeed(d);
        cadencePoint.setDistance(d2);
//        pressure = cadencePoint.getPressure();
//        int temperature2 = cadencePoint.getTemperature();
//        if (pressure > 0) {
//        }
//        if (cadencePoint.getSumCadence() == -1) {
//        }
//        if (cadencePoint.getCadenceTime() == -1) {
//        }
//        if (cadencePoint.getSumWheel() == -1) {
//        }
//        if (cadencePoint.getWheelTime() == -1) {
//        }
        this.lastCadencePoint = cadencePoint;
        return true;
    }

    public void release() {
        this.lastCadencePoint = null;
        this.lastValidCadenceTime = 0L;
        this.lastValidWheelTime = 0L;
        this.ptAltitudeCalc.reset();
    }
}
